package com.whmall.service.crm.provider;

import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.component.enumeration.CompanyLogOperationTypeEnum;
import com.whmall.component.enumeration.OperatorTypeEnum;
import com.whmall.component.provider.AbstractSameProvider;
import com.whmall.service.crm.api.CompanyConfigApi;
import com.whmall.service.crm.entity.dto.company.CompanyConfigDTO;
import com.whmall.service.crm.entity.dto.company.CompanyConfigQueryCondition;
import com.whmall.service.crm.enumeration.CompanyConfigKeyEnum;
import com.whmall.service.crm.module.company.dao.CompanyLogMapper;
import com.whmall.service.crm.module.company.entity.CompanyConfigDO;
import com.whmall.service.crm.module.company.entity.CompanyLogDO;
import com.whmall.service.crm.module.company.service.CompanyConfigService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@DubboService(interfaceClass = CompanyConfigApi.class)
public class CompanyConfigProvider extends AbstractSameProvider<CompanyConfigService, CompanyConfigQueryCondition> implements CompanyConfigApi {

    private final CompanyLogMapper companyLogMapper;

    /**
     * 根据key查询单位下的配置
     *
     * @param key
     * @param companyId 单位id
     * @return
     */
    @Override
    public CompanyConfigDTO getByKey(String key, Long companyId) {
        return service.getByKey(key, companyId);
    }

    @Override
    public List<CompanyConfigDTO> findByKey(String key) {
        return service.findByKey(key);
    }

    @Override
    public List<CompanyConfigDTO> findByKey(String key, Collection<Long> companyIds) {
        return service.findByKey(key, companyIds);
    }

    /**
     * 新增
     *
     * @param companyConfigDTO
     */
    @Override
    public void addConfig(CompanyConfigDTO companyConfigDTO, OperatorReqDTO operator) {
        CompanyConfigDO companyConfigDO = new CompanyConfigDO();
        BeanUtils.copyProperties(companyConfigDTO, companyConfigDO);
        service.save(companyConfigDO);
        addLog(companyConfigDTO, operator);
    }

    /**
     * 修改
     *
     * @param companyConfigDTO
     */
    @Override
    public void updateById(CompanyConfigDTO companyConfigDTO, OperatorReqDTO operator) {
        CompanyConfigDO companyConfigDO = new CompanyConfigDO();
        BeanUtils.copyProperties(companyConfigDTO, companyConfigDO);
        service.updateById(companyConfigDO);
        addLog(companyConfigDTO, operator);
    }

    public void addLog(CompanyConfigDTO companyConfigDTO, OperatorReqDTO operator) {
        String desc = companyConfigDTO.getValue().equals("1") ? "打开" : "关闭";
        if (companyConfigDTO.getKey().equals("has_depobipcopt")) {
            // 导出采购订单
            desc = desc + "是否在PC端个人中心显示\"导出采购订单\"按钮";
        } else if (companyConfigDTO.getKey().equals("has_desobipcopt")) {
            // 导出销售订单
            desc = desc + "是否在PC端个人中心显示\"导出销售订单\"按钮";
        }
        CompanyLogDO logDO = CompanyLogDO.builder()
                .companyId(companyConfigDTO.getCompanyId())
                .createType(OperatorTypeEnum.EMPLOYEE.getValue())
                .createId(operator.getOperatorId())
                .operationType(CompanyLogOperationTypeEnum.PC_SETTING.getValue())
                .operationDesc1(desc)
                .createTime(new Date())
                .ip(operator.getIp())
                .build();
        companyLogMapper.insertSelective(logDO);
    }

    @Override
    public Map<String, String> getConfig(Long companyId, Collection<String> keys) {
        return service.getConfig(companyId, keys);
    }

    @Override
    public Map<Long, Map<String, String>> getConfig(Collection<Long> companyIds, Collection<CompanyConfigKeyEnum> keys) {
        return service.getConfig(companyIds, keys);
    }

    @Override
    public String getValue(Long companyId, CompanyConfigKeyEnum key) {
        return service.getValue(companyId, key);
    }
}
